#ifndef MATLAB_MEX_FILE

/* Includes -------------------------------------------------------------*/
#include "icmu.h"


/* Private Functions ----------------------------------------------------*/
void Mcbspa_Init(void)
{
    EALLOW;
    
    /* Enable McBSPA Clock */
    SysCtrlRegs.PCLKCR0.bit.MCBSPAENCLK = 1;

    /* Configure pin muxing */
    GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 2;
    GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 2;
    GpioCtrlRegs.GPAMUX2.bit.GPIO22 = 2;

    GpioCtrlRegs.GPAPUD.bit.GPIO20  = 0;
    GpioCtrlRegs.GPAPUD.bit.GPIO21	= 1;
    GpioCtrlRegs.GPAPUD.bit.GPIO22	= 0;

    GpioCtrlRegs.GPAQSEL2.bit.GPIO21 = 3;
    
    EDIS;
    
    /* Configure McBSPA */
	McbspaRegs.SPCR2.bit.FRST			= 0;		// Reset Frame-Sync logic
	McbspaRegs.SPCR2.bit.GRST			= 0;		// Reset sample rate generator
	McbspaRegs.SPCR2.bit.XRST 			= 0;		// Reset transmitter
	McbspaRegs.SPCR1.bit.RRST			= 0;		// Reset receiver

	McbspaRegs.SPCR1.bit.DLB			= 0;		// Disable digital loopback mode

	McbspaRegs.PCR.bit.FSXP				= 1;		// The FSX pin (i.e., the STE pin of SPI) is active LOW
	McbspaRegs.SPCR1.bit.CLKSTP			= 3;		// Enable clock stop mode with clock delay, McBSP will work as SPI master.
	McbspaRegs.PCR.bit.CLKXP			= 1;		// CLK signal is HIGH on idle
	McbspaRegs.PCR.bit.CLKRP			= 1;		//

	McbspaRegs.RCR1.bit.RWDLEN1			= 4;     	// 24-bit word
    McbspaRegs.XCR1.bit.XWDLEN1			= 4;     	// 24-bit word
	McbspaRegs.XCR2.bit.XDATDLY			= 1;		// Transmit delay: 1-bit data delay
	McbspaRegs.RCR2.bit.RDATDLY			= 1;		// Receive dalay: 1-bit data delay

	McbspaRegs.PCR.bit.SCLKME			= 0;		// LSPCLK is selected as input clock of sample rate generator (SCLKME = 0, CLKSM = 1)
	McbspaRegs.SRGR2.bit.CLKSM			= 1;		//
	McbspaRegs.SRGR1.bit.CLKGDV			= 17;		// CLKG frequency = LSPCLK / (CLKGDV + 1) = 90MHz / (17 + 1) = 5MHz

	McbspaRegs.PCR.bit.CLKXM			= 1;		// Internal CLKX is generated by sample rate generator, MCLKX pin will output the internal CLKX signal
	McbspaRegs.PCR.bit.CLKRM			= 0;		// Internal receive clock is supplied by MCLKR pin, which is an input pin
	McbspaRegs.PCR.bit.FSXM				= 1;		// Tx frame sync is generated internally by sample rate generator
	McbspaRegs.PCR.bit.FSRM				= 1;		// Rx frame sync is supplied by sample rate generator

	McbspaRegs.SRGR2.bit.FSGM			= 0;		// The transmitter drives a frame-synchronization pulse on the FSX pin every time data is transferred from DXR1 to XSR1

	McbspaRegs.SPCR2.bit.GRST			= 1;		// Enable sample rate generator
	delay_loop();                                   // Wait at least 2 SRG clock cycles
	McbspaRegs.SPCR2.bit.XRST 			= 1;		// Release transmitter from reset
	McbspaRegs.SPCR1.bit.RRST			= 1;		// Release receiver from reset
	McbspaRegs.SPCR2.bit.FRST			= 1;		// Release Frame-Sync logic from reset
}


Uint32 Icmu_GetPosition(void)
{
	SSI_RAWDATA	RawData;

	/* Wait for the transmitter to be ready to accept new data */
	while(!(McbspaRegs.SPCR2.bit.XRDY));

	/* Write dummy bytes to the DXR register to start transmission */
	McbspaRegs.DXR2.all = 0x0000;
	McbspaRegs.DXR1.all = 0x0000;

	/* Wait for the data ready to be read */
	while(!(McbspaRegs.SPCR1.bit.RRDY));

	/* Read data from iC-MU*/
	RawData.words.Word_1 = McbspaRegs.DRR2.all;
	RawData.words.Word_0 = McbspaRegs.DRR1.all;

	/* Return the encoder position */
	return (Uint32)RawData.bits.Position;
}

#endif /* MATLAB_MEX_FILE */